import {
  Body,
  Controller,
  Post,
  Get,
  Param,
  ParseIntPipe,
  Request
} from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { UserService } from './user.service'

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('login')
  async userLogin(@Body('code') code: string) {
    const result = await this.userService.userLogin(code)
    return {
      statusCode: 200,
      message: 'OK',
      data: {
        ...result
      }
    }
  }

  @Get(':id')
  async getUserById(@Param('id', ParseIntPipe) id: number) {
    const user = await this.userService.getUserById(id)
    return {
      statusCode: 200,
      message: 'OK',
      data: {
        user
      }
    }
  }

  @Post('update')
  async updateUser(@Request() req, @Body() data: Prisma.UserUpdateInput) {
    const token = req.headers.authorization.substring(7)
    console.log(data)
    const user = await this.userService.updateUser(token, data)
    return {
      statusCode: 200,
      message: 'OK',
      data: {
        user
      }
    }
  }
}
